Conditions | 1 |
Paths | 1 |
Total Lines | 130 |
Code Lines | 98 |
Lines | 0 |
Ratio | 0 % |
Changes | 11 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | var TableInitSetColumnsHoverEffect = function(tContainer, tableId){ |
||
232 | var TableSingleton = (function(){ |
||
233 | // Instance stores a reference to the Singleton |
||
234 | var instance; |
||
235 | function initInstance(){ |
||
236 | // Singleton |
||
237 | // Private methods and variables |
||
238 | var tail = null; |
||
239 | function LoadData(tableContainer, rq){ |
||
240 | if(tail!==null){ tail.abort();} |
||
241 | TableHelperSetVisability(tableContainer, false); |
||
242 | var xmlhttp = window.XMLHttpRequest ? |
||
243 | new XMLHttpRequest() : /* code for IE7+, Firefox, Chrome, Opera, Safari */ |
||
244 | new window.ActiveXObject("Microsoft.XMLHTTP");/*code for IE6, IE5 */ |
||
245 | xmlhttp.onreadystatechange = function(){ |
||
246 | if(xmlhttp.readyState === 4 && xmlhttp.status === 200){ |
||
247 | Draw(tableContainer, JSON.parse(xmlhttp.responseText)); |
||
248 | TableHelperSetVisability(tableContainer, true); |
||
249 | instance.LoadEndCalback(tableContainer); |
||
250 | } |
||
251 | }; |
||
252 | xmlhttp.open("GET", RequestToUrl(rq), true); |
||
253 | xmlhttp.send(); |
||
254 | tail = xmlhttp; //put at tail to can abort later any previous |
||
255 | } |
||
256 | function Init(tableId){ |
||
257 | var tContainer = document.getElementById(tableId); |
||
258 | if(iePrior(9)){ |
||
259 | TableInitSetColumnsHoverEffect(tContainer, tableId); |
||
260 | } |
||
261 | var tfoot = tContainer.getElementsByTagName("tfoot")[0]; |
||
262 | TableHelperProcessPaginationLinks(tfoot); |
||
263 | } |
||
264 | function Draw(tableContainer, d){ |
||
265 | TableHelperDraw_Section(tableContainer, d.body); |
||
266 | TableHelperDraw_Section(tableContainer, d.footer, "tfoot"); |
||
267 | if(instance.rq !== null){ |
||
268 | var hover = document.getElementById(instance.rq.tableId) |
||
269 | .getElementsByTagName("th")[instance.rq.colNo].lang; |
||
270 | if(hover){ |
||
271 | instance.ColumnHover(tableContainer, instance.rq.colNo); |
||
272 | } |
||
273 | } |
||
274 | } |
||
275 | |||
276 | var BuildRequest = TableHelperBuildRequest; |
||
277 | var FilterGetTableId = TableHelperFilterGetTableId; |
||
278 | var GoPageGetNo = TableHelperGoPageGetNo; |
||
279 | var getParent = TableHelperGetParent; |
||
280 | var iePrior = TableHelperIePrior; |
||
281 | var RequestToUrl = TableHelperRequestToUrl; |
||
282 | |||
283 | return { |
||
284 | rq: null, |
||
285 | strAsc: String.fromCharCode(9650), //▲ |
||
286 | strDesc: String.fromCharCode(9660),//▼ |
||
287 | ReloadData: function(tableId){ |
||
288 | var request = {}; |
||
289 | BuildRequest(request, tableId, this.strDesc); |
||
290 | LoadData(tableId, request); |
||
291 | }, |
||
292 | Filter: function(field){ |
||
293 | var crntTableId = FilterGetTableId(field); |
||
294 | if(crntTableId !== null){ |
||
295 | var request = {}; |
||
296 | var exRq = this.rq; |
||
297 | BuildRequest(request, crntTableId, this.strDesc); |
||
298 | if(exRq === null || |
||
299 | request.filter !== exRq.filter || |
||
300 | request.filterBy !== exRq.filterBy |
||
301 | ){ |
||
302 | LoadData(crntTableId, request); |
||
303 | } |
||
304 | } |
||
305 | }, |
||
306 | GoPage: function(lnk){ |
||
307 | var request = {}; |
||
308 | var crntTableId = getParent(lnk, "table").getAttribute("id"); |
||
309 | BuildRequest(request, crntTableId, this.strDesc); |
||
310 | request.pageNo = GoPageGetNo(lnk, crntTableId); |
||
311 | LoadData(crntTableId, request); |
||
312 | return false; |
||
313 | }, |
||
314 | Export: function(lnk, eType){ |
||
315 | var request = {}; |
||
316 | var crntTableId = getParent(lnk, "table").getAttribute("id"); |
||
317 | BuildRequest(request, crntTableId, this.strDesc); |
||
318 | request.exportType = ["CSV", "Excel"].indexOf(eType) >= 0 ? |
||
319 | eType : |
||
320 | "csv"; |
||
321 | window.open(RequestToUrl(request)); |
||
322 | return false; |
||
323 | }, |
||
324 | Sort: function(colNo, lnk){ |
||
325 | var request = {}; |
||
326 | var crntTableId = getParent(lnk, "table").getAttribute("id"); |
||
327 | BuildRequest(request, crntTableId, this.strDesc); |
||
328 | if(Math.round(colNo) === request.colNo){ |
||
329 | request.colOrd = (request.colOrd === "asc" ? "desc" : "asc"); |
||
330 | }else{ |
||
331 | request.colNo = Math.round(colNo); |
||
332 | request.colOrd = "asc"; |
||
333 | } |
||
334 | LoadData(crntTableId, request); |
||
335 | /* Clear and add new sort arrow */ |
||
336 | var headSpans = getParent(lnk, "thead").getElementsByTagName("span"); |
||
337 | var length = headSpans.length; |
||
338 | for(var i = 0; i < length; i++){ |
||
339 | headSpans[i].innerHTML = ""; |
||
340 | } |
||
341 | lnk.getElementsByTagName("span")[0].innerHTML = (request.colOrd === "desc" ? this.strDesc : this.strAsc); |
||
342 | }, |
||
343 | ColumnHover: function(tableContainer, index){ |
||
344 | if(!iePrior(9)){ |
||
345 | TableHelperColumnHover.call(this, tableContainer, index); |
||
346 | } |
||
347 | }, |
||
348 | LoadEndCalback: function(){},/*Allows override: function(tableId){if(tableId){...}}*/ |
||
349 | init: Init |
||
350 | }; |
||
351 | } |
||
352 | return { |
||
353 | //Get the Singleton instance if one exists, or create one if it doesn't |
||
354 | getInstance: function(){ |
||
355 | if(!instance){ |
||
356 | instance = initInstance(); |
||
357 | } |
||
358 | return instance; |
||
359 | } |
||
360 | }; |
||
361 | })(); |
||
362 | var table = TableSingleton.getInstance(); |
||
363 |
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: